<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别 " prop="sex">
        <el-select
          v-model="queryParams.sex"
          placeholder="请选择性别 "
          clearable
          size="small"
        >
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态 " prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态 "
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="值班状态" prop="onDutyStatus">
        <el-select
          v-model="queryParams.onDutyStatus"
          placeholder="请选择值班状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in onDutyStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:driver:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:driver:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">
        <el-button
          v-hasPermi="['manager:driver:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="driverList"
      @selection-change="handleSelectionChange"
      height="61vh"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="司机标识" align="center" prop="driverID" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column
        label="性别 "
        align="center"
        prop="sex"
        :formatter="sexFormat"
      />
      <el-table-column label="手机号" align="center" prop="phone" width="110" />
      <el-table-column label="地址" align="center" prop="address" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="状态 "
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      <el-table-column
        label="值班状态"
        align="center"
        prop="onDutyStatus"
        :formatter="onDutyStatusFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['manager:driver:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >查看</el-button
          >
          <el-button
            v-hasPermi="['manager:driver:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-lightning"
            style="color: #ff0000"
            v-show="!(scope.row.status == 1)"
            @click="handelLight(scope.row)"
          >
            停用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改司机管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      :width="interlayerWidth"
      :before-close="handleDialogClose"
      append-to-body
    >
      <el-row class="dialog-title">
        <el-col :span="24">
          <el-button :type="elButtonType" @click="submitForm(modifyCont)">{{
            modifyCont
          }}</el-button>
        </el-col>
      </el-row>

      <el-form
        style="margin-top: 20px;"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入姓名"
                v-if="isShow"
              />
              <div class="dialog-text" v-if="!isShow">{{ form.name }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input
                v-model="form.age"
                placeholder="请输入年龄"
                v-if="isShow"
              />
              <div class="dialog-text" v-if="!isShow">{{ form.age }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别 ">
              <el-select
                v-model="form.sex"
                placeholder="请选择性别 "
                v-if="isShow"
              >
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                />
              </el-select>
              <div class="dialog-text" v-if="!isShow">
                {{ sexFormat(form) }}
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input
                v-model="form.phone"
                placeholder="请输入手机号"
                v-if="isShow"
              />
              <div class="dialog-text" v-if="!isShow">{{ form.phone }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="form.address"
                placeholder="请输入地址"
                v-if="isShow"
              />
              <div class="dialog-text" v-if="!isShow">{{ form.address }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态 ">
          <el-radio-group v-model="form.status" v-if="isShow">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
          <div class="dialog-text" v-if="!isShow">
            {{ statusFormat(form) }}
          </div>
        </el-form-item>
        <el-form-item label="值班状态">
          <el-radio-group v-model="form.onDutyStatus" v-if="isShow">
            <el-radio
              v-for="dict in onDutyStatusOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
          <div class="dialog-text" v-if="!isShow">
            {{ onDutyStatusFormat(form) }}
          </div>
        </el-form-item>
      </el-form>

      <div v-show="tableShow">
        <div><h3>历史调度记录</h3></div>
        <el-table v-loading="loading" :data="dispathList">
          <el-table-column label="调度流水号" align="center" prop="doctorID" />
          <el-table-column
            label="出车时间"
            align="center"
            prop="name"
            width="110"
          />
          <el-table-column label="医生" align="center" prop="age" />
          <el-table-column
            label="护士"
            align="center"
            prop="sex"
            :formatter="sexFormat"
          />
          <el-table-column
            label="接车地点"
            align="center"
            prop="phone"
            width="110"
          />
          <el-table-column label="收治医院" align="center" prop="depart" />
          <el-table-column
            label="患者病历"
            align="center"
            prop="title"
            width="100"
          />
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <!--      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import {
  listDriver,
  getDriver,
  delDriver,
  addDriver,
  updateDriver,
  exportDriver,
  lightDriver
} from "@/api/biz/driver";

export default {
  name: "Driver",
  data() {
    return {
      isShow: false,
      //按钮样式
      elButtonType: "",
      // 修改文本
      modifyCont: "编辑资料",
      dispathList: [],
      tableShow: false,
      interlayerWidth: "780px",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 司机管理表格数据
      driverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别 字典
      sexOptions: [],
      // 状态 字典
      statusOptions: [],
      // 值班状态字典
      onDutyStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        address: undefined,
        age: undefined,
        sex: undefined,
        status: undefined,
        onDutyStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            message: "姓名不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/,
            message: "姓名格式错误",
            trigger: "blur"
          }
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur"
          },
          {
            pattern: /^1(3|4|5|7|8|9)\d{9}$/,
            message: "手机号格式错误",
            trigger: "blur"
          }
        ],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
        onDutyStatus: [
          { required: true, message: "值班状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("common_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("driver_duty_status").then(response => {
      this.onDutyStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询司机管理列表 */
    getList() {
      this.loading = true;
      listDriver(this.queryParams).then(response => {
        this.driverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 性别 字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 状态 字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 值班状态字典翻译
    onDutyStatusFormat(row, column) {
      return this.selectDictLabel(this.onDutyStatusOptions, row.onDutyStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        driverID: undefined,
        name: undefined,
        phone: undefined,
        address: undefined,
        remark: undefined,
        age: undefined,
        sex: 0,
        status: 0,
        onDutyStatus: 0,
        uid: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.driverID);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /**  冻结操作 */
    handelLight(row) {
      const driverID = row.driverID || this.ids;
      this.$confirm(
        '是否确认停用司机管理编号为"' + driverID + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return lightDriver(driverID);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("停用成功");
        })
        .catch(function() {});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加司机管理";
      this.modifyCont = "保存";
      this.elButtonType = "primary";
      this.isShow = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.interlayerWidth = "1200px";
      this.tableShow = true;
      const driverID = row.driverID || this.ids;
      getDriver(driverID).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "司机详情";
      });
    },
    /** 提交按钮 */
    submitForm: function(e) {
      if (e == "编辑资料") {
        this.modifyCont = "保存";
        this.elButtonType = "primary";
        // this.reset();
        this.isShow = true;
        return true;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.driverID !== undefined) {
            updateDriver(this.form).then(response => {
              if (response.code === 200) {
                this.getList();
                this.modify(e);
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addDriver(this.form).then(response => {
              if (response.code === 200) {
                this.getList();
                this.modify(e);
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const driverIDs = row.driverID || this.ids;
      this.$confirm(
        '是否确认删除司机管理编号为"' + driverIDs + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delDriver(driverIDs);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有司机管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportDriver(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    },
    modify(e) {
      this.isShow = !this.isShow;
      if (e == "编辑资料") {
        this.modifyCont = "保存";
        this.elButtonType = "primary";
        // this.reset();
        this.open = false;
      } else {
        // 新增
        this.elButtonType = "";
        setTimeout(() => {
          this.modifyCont = "编辑资料";
          // this.reset();
          this.open = false;
        }, 300);
      }
    },
    //关闭弹窗
    handleDialogClose() {
      // this.isShow = true;
      this.open = false;
      this.modifyCont = "编辑资料";
      this.elButtonType = "";
      this.isShow = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.logoImg {
  width: 146px !important;
  height: 146px !important;
  border-radius: 6px;
}
.dialog-title {
  width: 100%;

  > div {
    line-height: 16px;
    font-size: 16px;
    text-align: right;
    cursor: pointer;
    color: #333;
  }
}
.dialog-text {
  width: 200px;
}
/* 分页 */
.pagination-container {
  height: 80px;
  margin-top: 0px;
  padding: 23px 20px !important;
}
/* 分页  end */
</style>
